package org.readium.r2.testapp.data.db

import androidx.annotation.ColorInt
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import kotlinx.coroutines.flow.Flow
import org.readium.r2.testapp.data.model.Vocabulary

@Dao
interface DictionaryDao {

    /**
     * Inserts a vocabulary
     * @param vocabulary The vocabulary to insert
     * @return the ENTRY of the vocabulary that was added (primary key)
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertVocabulary(vocabulary: Vocabulary)

    /**
     * Deletes a vocabulary
     * @param entry The entry of the word
     */
    @Query("DELETE FROM " + Vocabulary.TABLE_NAME + " WHERE " + Vocabulary.ENTRY + " = :entry")
    suspend fun deleteVocabulary(entry: String)

    /**
     * Retrieve a Vocabulary from its ENTRY.
     */
    @Query("SELECT * FROM " + Vocabulary.TABLE_NAME + " WHERE " + Vocabulary.ENTRY + " = :entry")
    suspend fun get(entry: String): Vocabulary?

    /**
     * search for a vocabulary by its ENTRY
     */
    @Query("SELECT * FROM " + Vocabulary.TABLE_NAME + " WHERE " + Vocabulary.ENTRY + " IS :entry COLLATE NOCASE")
    fun searchVocabulary(entry: String): Flow<Vocabulary>

    /**
     * Retrieve all MARKED Vocabularies
     * @return List of Vocabularies as Flow
     */
    @Query("SELECT * FROM " + Vocabulary.TABLE_NAME + " WHERE " + Vocabulary.MARKED + " = 1")
    fun getAllVocabularyMarked(): Flow<List<Vocabulary>>

    /**
     * mark and unmark a vocabulary by its ENTRY
     */
    @Query("UPDATE " + Vocabulary.TABLE_NAME + " SET " + Vocabulary.MARKED + " = :isMarked WHERE " + Vocabulary.ENTRY + " = :entry")
    suspend fun markVocabulary(entry: String, isMarked: Boolean)
}
